home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Microsoft / Expression Blend / Blend.en.msi / Sparkle.AStudio.Window1.xaml.cs.en < prev    next >
Encoding:
Text (UTF-16)  |  2007-03-19  |  17.1 KB  |  247 lines

  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Navigation;
  12. using System.Collections.Generic;
  13. using System.Windows.Ink;
  14. using Microsoft.Win32;
  15.  
  16. namespace UntitledProject1
  17. {
  18.     public partial class Window1
  19.     {
  20.         public static readonly RoutedCommand ExitCommand = new RoutedCommand("Exit", typeof(Window1));
  21.  
  22.         private FlipBook flipBook = null;
  23.  
  24.         // Current pencil selected.
  25.         private FrameworkElement currentPencil;
  26.         
  27.         // Keep the original position of the selected pencil to be able
  28.         // return it back in place when another one is selected.
  29.         private Point keepDockPosPencil;
  30.  
  31.         // Uses mouse position to update current pencil selection.
  32.         private double mouseX, mouseY;
  33.  
  34.         #region Initialization
  35.  
  36.         public Window1()
  37.         {
  38.             this.DataContext = this;
  39.             this.InitializeComponent();
  40.             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New, this.NewFlipBook));
  41.             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, this.OpenFlipBook));
  42.             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.SaveAs, this.SaveFlipBookAs));
  43.             this.CommandBindings.Add(new CommandBinding(Window1.ExitCommand, this.Exit));
  44.         }
  45.  
  46.         private void NewFlipBook(object sender, EventArgs e)
  47.         {
  48.             this.flipBook.Clear();
  49.         }
  50.  
  51.         /// <summary>
  52.         /// After all the elements are ready, bind the CLR Object Data Source
  53.         /// This is an alternative to: protected override void OnInitialized(EventArgs e)
  54.         /// since when OnLoaded fires all the elements are initialized.
  55.         /// </summary>
  56.         private void OnLoaded(object sender, RoutedEventArgs e)
  57.         {
  58.             ObjectDataProvider odp = FindResource("FlipBookDS") as ObjectDataProvider;
  59.             if (odp != null)
  60.             {
  61.                 flipBook = odp.Data as FlipBook;
  62.             }
  63.             currentPencil = colorBlack;
  64.             ActivatePencil(currentPencil);
  65.         }
  66.  
  67.         #endregion
  68.  
  69.         #region File
  70.  
  71.         private void OpenFlipBook(object caller, EventArgs e)
  72.         {
  73.             OpenFileDialog fileDialog = new OpenFileDialog();
  74.             fileDialog.Title = "Open Animation";
  75.             fileDialog.RestoreDirectory = true;
  76.             fileDialog.InitialDirectory = "../../Animations";
  77.             fileDialog.Filter = "Flipbook Animations (*.fba)|*.fba";
  78.             bool? dialogResult = fileDialog.ShowDialog(this);
  79.  
  80.             if (dialogResult.HasValue && dialogResult.Value)
  81.             {
  82.                 try
  83.                 {
  84.                     this.flipBook.Load(fileDialog.FileName);
  85.                 }
  86.                 catch (Exception exception)
  87.                 {
  88.                     string error = String.Format("Cannot open file '{0}'.\n{1}", fileDialog.FileName, exception.Message);
  89.                     MessageBox.Show(this, error, "Cannot open file", MessageBoxButton.OK, MessageBoxImage.Error);
  90.                 }
  91.             }
  92.         }
  93.  
  94.         private void SaveFlipBookAs(object caller, EventArgs e)
  95.         {
  96.             SaveFileDialog fileDialog = new SaveFileDialog();
  97.             fileDialog.Title = "Save Animation As";
  98.             fileDialog.RestoreDirectory = true;
  99.             fileDialog.InitialDirectory = "../../Animations";
  100.             fileDialog.Filter = "Flipbook Animations (*.fba)|*.fba";
  101.             bool? dialogResult = fileDialog.ShowDialog(this);
  102.  
  103.             if (dialogResult.HasValue && dialogResult.Value)
  104.             {
  105.                 try
  106.                 {
  107.                     this.flipBook.Save(fileDialog.FileName);
  108.                 }
  109.                 catch (Exception exception)
  110.                 {
  111.                     string error = String.Format("Cannot save file '{0}'.\n{1}", fileDialog.FileName, exception.Message);
  112.                     MessageBox.Show(this, error, "Cannot save file", MessageBoxButton.OK, MessageBoxImage.Error);
  113.                 }
  114.             }
  115.         }
  116.  
  117.         private void Exit(object sender, EventArgs e)
  118.         {
  119.             Application.Current.Shutdown();
  120.         }
  121.  
  122.         #endregion
  123.  
  124.         #region Pencil
  125.  
  126.         /// <summary>
  127.         /// When the user clicks one of the pencils, change the selection.
  128.         /// </summary>
  129.         private void OnGrabPencil(object sender, RoutedEventArgs e)
  130.         {
  131.             Canvas.SetZIndex(currentPencil, 0);
  132.             Canvas.SetLeft(currentPencil, keepDockPosPencil.X);
  133.             Canvas.SetTop(currentPencil, keepDockPosPencil.Y);
  134.             currentPencil.Width = 55;
  135.             currentPencil.Visibility = Visibility.Visible;
  136.             this.MouseMove -= new MouseEventHandler(DragPencil);
  137.             currentPencil = sender as FrameworkElement;
  138.             ActivatePencil((FrameworkElement)sender);
  139.         }
  140.  
  141.         /// <summary>
  142.         /// This method updates the selection with the new mouse.
  143.         /// </summary>
  144.         void ActivatePencil(FrameworkElement pencil)
  145.         {
  146.             Button currentSelected = pencil as Button;
  147.             if (flipBook != null) flipBook.InkColor = (Color)ColorConverter.ConvertFromString(currentSelected.Background.ToString());
  148.             Canvas.SetZIndex(currentPencil, 10);
  149.             currentPencil.Width = 177;
  150.             keepDockPosPencil = new Point(Canvas.GetLeft(currentPencil), Canvas.GetTop(currentPencil));
  151.             UpdatePencilPosition();
  152.             this.MouseMove += new MouseEventHandler(DragPencil);
  153.         }
  154.  
  155.         void DragPencil(object sender, MouseEventArgs e)
  156.         {
  157.             UpdatePencilPosition();
  158.         }
  159.  
  160.         private void UpdatePencilPosition()
  161.         {
  162.             UpdateMousePos();
  163.             Canvas.SetLeft(currentPencil, mouseX - 29);
  164.             Canvas.SetTop(currentPencil, mouseY + 25);
  165.         }
  166.  
  167.         private void UpdateMousePos()
  168.         {
  169.             Point mousePos = Mouse.GetPosition(studio);
  170.             mouseX = mousePos.X;
  171.             mouseY = mousePos.Y;
  172.         }
  173.  
  174.         private void ShowPencil()
  175.         {
  176.             currentPencil.Visibility = Visibility.Visible;
  177.         }
  178.         private void HidePencil()
  179.         {
  180.             currentPencil.Visibility = Visibility.Hidden;
  181.         }
  182.  
  183.         private void OnShowPencil(object sender, MouseEventArgs e)
  184.         {
  185.             ShowPencil();
  186.         }
  187.  
  188.         private void OnHidePencil(object sender, MouseEventArgs e)
  189.         {
  190.             HidePencil();
  191.         }
  192.  
  193.         #endregion
  194.  
  195.         #region Stroke Event
  196.  
  197.         /// <summary>
  198.         /// When a new stroke size is selected it triggers this event.
  199.         /// </summary>
  200.         private void OnChangeStroke(object sender, RoutedEventArgs e)
  201.         {
  202.             RadioButton selected = sender as RadioButton;
  203.             if (flipBook != null) flipBook.InkWidth = selected.FontSize;
  204.         }
  205.  
  206.         #endregion
  207.  
  208.         #region RepeatMode Event
  209.  
  210.         /// <summary>
  211.         /// Sets the current repeatMode, based on the menu item selected.
  212.         /// Once - Plays the animations one time.
  213.         /// Loop - Play in a infinite loop.
  214.         /// Bounce - Plays and reverses the animation in an infinite loop.
  215.         /// </summary>
  216.         private void OnRepeatMode(object sender, RoutedEventArgs e)
  217.         {
  218.             if (flipBook != null)
  219.             {
  220.                 MenuItem el = sender as MenuItem;
  221.                 RepeatMode mode = new RepeatMode();
  222.                 switch (el.Header.ToString())
  223.                 {
  224.                     case "_Once":
  225.                         mode = RepeatMode.Once;
  226.                         itemMenuLoop.IsChecked = false;
  227.                         itemMenuBounce.IsChecked = false;
  228.                         break;
  229.                     case "_Loop":
  230.                         mode = RepeatMode.Loop;
  231.                         itemMenuOnce.IsChecked = false;
  232.                         itemMenuBounce.IsChecked = false;
  233.                         break;
  234.                     case "_Bounce":
  235.                         mode = RepeatMode.Bounce;
  236.                         itemMenuOnce.IsChecked = false;
  237.                         itemMenuLoop.IsChecked = false;
  238.                         break;
  239.                 }
  240.                 flipBook.RepeatMode = mode;
  241.             }
  242.         }
  243.  
  244.         #endregion
  245.  
  246.     }
  247. }